#ifndef __HTTP_H_
#define __HTTP_H_
#include <Windows.h>
#include <WinInet.h>
#include <string>
#pragma comment(lib, "Wininet.lib")

class HttpService final
{
public:
	HttpService(){}
	~HttpService()
	{
		Close();
	}
	HttpService(const HttpService&) = delete;
	HttpService& operator= (const HttpService&) = delete;

public:
	BOOL init(LPCTSTR lpszAgent = "WinInetGet/0.1", LPCTSTR lpszProxyName = NULL, LPCTSTR lpszProxyByPass = NULL);
	BOOL connect(LPCTSTR lpszServerName, INTERNET_PORT nServerPort, LPCTSTR lpszUserName = NULL, LPCTSTR lpszPassword = NULL);
	BOOL ConnectAndCreateRequest(LPCTSTR lpszVerb, std::string url, LPCTSTR lpszVersion = "HTTP/1.1", DWORD dwFlag = INTERNET_FLAG_NO_CACHE_WRITE);
	BOOL CreateRequest(LPCTSTR lpszVerb, LPCTSTR lpszObjectName, LPCTSTR lpszVersion = "HTTP/1.1", DWORD dwFlag = INTERNET_FLAG_NO_CACHE_WRITE);
	BOOL AddHeader(LPCTSTR lpszHeaders);
	BOOL SendRequest(LPVOID lpOptional = 0, DWORD dwOptionalLength = 0, LPCTSTR lpszHeaders = NULL, DWORD dwHeadersLength = 0);
	BOOL SendData(LPCVOID lpBuffer, DWORD dwNumberOfBytesToWrite);
	void RecvData(std::string&);
	void RecvDataWithHeader(std::string&);
	void CloseRequest();
	void CloseSession();
	void Close();

private:
	BOOL parseURL(std::string& url, std::string& proto, std::string& hostname, std::string& path, INTERNET_PORT& port);
private:
	HINTERNET	m_hInternet;
	HINTERNET	m_hSession;
	HINTERNET	m_hRequest;
};
#endif